const app = getApp();
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    tag_list: [
      {
        title:'音乐',
        color:'cyan'
      },
      {
        title:'电影迷',
        color:'blue'
      },
      {
        title:'宅男',
        color:'green'
      },
      {
        title:'御姐',
        color:'olive'
      },
      {
        title:'侦探',
        color:'pink'
      },
      {
        title:'90后',
        color:'purple'
      },
      {
        title:'00后',
        color:'mauve'
      }
    ],
    color_list:['cyan','blue','green','olive'],
    member_tag:[],
    disabled:false,
    msg:'确认提交'
  },

  onShow:function(){
    let _this = this;
    _this.getUserInfo(); //获取账本
  },

  //选择按钮
  chose_this:function(e){
    let _this = this,
     chose_index = e.currentTarget.dataset.index,
     tag_list = _this.data.tag_list,
     member_tag = _this.data.member_tag;
     if(member_tag.length<3){
      member_tag.push(tag_list[chose_index].title);
      _this.setData({
        member_tag:member_tag
      })
     }else{
      wx.showToast({
        title: '只能贴三个标签哦',
        icon: 'none',
        duration: 2000
      })
     }
  },

  //删除标签
  del_tag:function(e){
    let _this = this,
    member_tag = _this.data.member_tag,
    del_index = e.currentTarget.dataset.index;
    member_tag.splice(del_index,1);
    _this.setData({
      member_tag:member_tag
    })
  },

  //获取个人信息:确认是否有登陆
  getUserInfo:function(){
    let _this = this;
    app._post('api/member/getUserInfo', {}, function(result) {
      if(result.code != 200){
        wx.navigateTo({
          url: '/pages/auth/auth'
        })
      }else{
        var member_tag = result.data.tag_list;
        if(member_tag.length > 0){
          _this.setData({
            member_tag:member_tag.split(',')
          })
        }
      }
    });
  },

  /*表单提交*/
  sub_form: function() {
    let _this = this,
      member_tag = _this.data.member_tag;
    // 表单验证
    if (member_tag.length == 0){
      app.showError('请至少选择一个标签');
      return false;
    }
    _this.setData({disabled:true}); //禁止重复点击提交按钮

    app._post('api/member/update_member_info',
      { member_tag:member_tag}, 
    function(result) {
      wx.hideToast()
      if(result.code == 200){
        app.showSuccess(result.msg);
        _this.setData({
          msg:'更改成功'
        })
      }else{
        app.showError(result.msg);
        _this.setData({
          msg:'更改失败'
        })
      }
    }, false, function() {
      // 解除禁用
      _this.setData({
        disabled: false
      });
    });
  },
})